<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="工单号" prop="docNo">
        <el-input
          v-model="queryParams.docNo"
          placeholder="请输入工单号"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->

  <ContentWrap>
    <div>
      <span>工单物料详情</span>
    </div>
    <el-tabs v-model="activeTab" @tab-click="handleTabClick">
      <el-tab-pane
        :label="item.label"
        :name="item.label"
        v-for="item in getIntDictOptions(DICT_TYPE.OPTICORE_MATE_TYPE)"
        :key="item.value"
      >
        <MaterialsTableList :dataList="filterList" />
      </el-tab-pane>
    </el-tabs>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ref, reactive } from 'vue'
import { MaterialUsageApi } from '@/api/opticore/materialusage'
import type { TabsPaneContext } from 'element-plus'
import MaterialsTableList from '@/views/opticore/print/components/MaterialsTableList.vue'

import {onMounted} from 'vue'

onMounted(()=>{
   activeTab.value = getIntDictOptions(DICT_TYPE.OPTICORE_MATE_TYPE)[0].label
  getMaterialByDocNo(getIntDictOptions(DICT_TYPE.OPTICORE_MATE_TYPE)[0].value + '')
})

const filterList = ref([])

const total = ref(0)
/** 已领用物料 列表 */
const activeTab = ref(getIntDictOptions(DICT_TYPE.OPTICORE_MATE_TYPE)[0].label) // 默认激活的tab

// 分别定义 ref
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  serialNo: undefined,
  batchNo: undefined,
  docNo: undefined,
  itemCode: undefined,
  itemName: undefined,
  itemSpecification: undefined,
  fiberPalletNo: undefined,
  qty: undefined,
  consumedQty: undefined,
  unit: undefined,
  materialType: undefined,
  parentId: undefined,
  createTime: []
})

const handleTabClick = (tab: TabsPaneContext) => {
  let fItem = getIntDictOptions(DICT_TYPE.OPTICORE_MATE_TYPE).find(
    (item) => item.label == tab.props.name
  )
  getMaterialByDocNo(fItem.value + '')
}

const getMaterialByDocNo = async (materialType: string) => {
  let data = {
    docNo: queryParams.docNo,
    materialType: materialType,
    pageNo: queryParams.pageNo,
    pageSize: queryParams.pageSize
  }
  const res = await MaterialUsageApi.getMaterialUsagePage(data)

  if (res) {
    filterList.value = res.list
  }
}

const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  activeTab.value = getIntDictOptions(DICT_TYPE.OPTICORE_MATE_TYPE)[0].label
  getMaterialByDocNo(getIntDictOptions(DICT_TYPE.OPTICORE_MATE_TYPE)[0].value + '')
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  // 保持在当前 tab
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
</script>
